import cv2 as cv
import numpy as np

def onChange(n):
    h_min = cv.getTrackbarPos("h_min", "mask")
    h_max = cv.getTrackbarPos("h_max", "mask")
    s_min = cv.getTrackbarPos("s_min", "mask")
    s_max = cv.getTrackbarPos("s_max", "mask")
    v_min = cv.getTrackbarPos("v_min", "mask")
    v_max = cv.getTrackbarPos("v_max", "mask")

    min = np.array([h_min, s_min, v_min])
    max = np.array([h_max, s_max, v_max])
    print(min, max)
    img_mask = cv.inRange(imgHsv, min, max)
    cv.imshow("mask", img_mask)
    imgWithMask = cv.bitwise_and(img, img, mask=img_mask)
    cv.imshow("img", imgWithMask)

img = cv.imread("assets/car2.jpg")
#img = cv.GaussianBlur(img, (11,11), 1)
imgHsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
#cv.imshow("img", img)
cv.namedWindow("mask", 1)
cv.imshow("mask", imgHsv)
cv.createTrackbar("h_min", "mask", 0, 360, onChange)
cv.createTrackbar("h_max", "mask", 360, 360, onChange)
cv.createTrackbar("s_min", "mask", 0, 255, onChange)
cv.createTrackbar("s_max", "mask", 255, 255, onChange)
cv.createTrackbar("v_min", "mask", 0, 255, onChange)
cv.createTrackbar("v_max", "mask", 255, 255, onChange)

onChange(0)

cv.waitKey(0)
cv.waitKey(500)
cv.destroyAllWindows()